<?php

	$query = "	SELECT
  					m.title".__FLANG." AS title,
  					m.outerlink,
  					m.params
  			 	FROM ".$_SQL_TABLE['menu']." m
  			 	INNER JOIN ".$_SQL_TABLE['menu_block']." mb ON mb.id = m.menu_block
  			 	WHERE
  			 		m.active = 'y'
  			 	AND	mb.active = 'y'
  			 	AND mb.code != 'langmenu'
  			 	ORDER BY mb.code, m.position";
  	$dbSet->open($query);
    $result = $dbSet->fetchRowsAll();
    $SiteMap = array();
    foreach($result as $n => $arr)
    {
  	  if($arr['outerlink'] != '')
  	  {
  	    $arr['code'] = str_replace($CONFIG['webPageFileType'], '', $arr['outerlink']);
  	    if(!preg_match("/^\//is", $arr['outerlink']) && !preg_match("/^http[s]*/is", $arr['outerlink']))
  	    {
  	      $arr['outerlink'] = $BaseURL.$arr['outerlink'];
  	    }
  	    elseif($arr['outerlink'] == '/')
  	    {
  	      $arr['outerlink'] = $BaseURL;
  	    }
  	    $result[$n]['link'] = $arr['link'] = $arr['outerlink'];
  	  }
  	  else
  	  {
  	    $result[$n]['link'] = $FrontMenu->makeMenuLink($arr['params']);
  	  }
  	  if($result[$n]['link'] == $_SERVER['REQUEST_URI'])
  	  {
  	    $result[$n]['currItem']	= 'y';
  	  }
      $item = array();
      $item['title'] = $arr['title'];
      $item['link'] = $arr['link'];
      $item['code'] = $arr['code'];
      $item['items'] = array();
      $SiteMap[] = $item;
    }

    /*$SiteMap[] = array(
    		'title' => $language['front']['regions'],
    		'code' => 'regions',
    		'link' => $BaseURL.'regions'.$CONFIG['webPageFileType'],
    		'items' => array(),
    );*/

    $SiteMap[] = array(
    		'title' => $language['front']['rubrics'],
    		'code' => 'rubrics',
    		'link' => $BaseURL.'rubrics'.$CONFIG['webPageFileType'],
    		'items' => array(),
    );

    $SiteMap[] = array(
    		'title' => $language['front']['keywords'],
    		'code' => 'tags',
    		'link' => $BaseURL.'tags'.$CONFIG['webPageFileType'],
    		'items' => array(),
    );

    $SiteMap[] = array(
    		'title' => 'RSS',
    		'code' => 'rss',
    		'link' => $BaseURL.'rss'.$CONFIG['webPageFileType'],
    		'items' => array(),
    );


    foreach($SiteMap as $k => $arr)
    {      if($arr['code'] == 'authors')
      {        $Authors = getTableAsArray($_SQL_TABLE['author'], 'name'.__FLANG, array("active = 'y'"), '', 'id,linkname,name'.__FLANG.' AS title');
        foreach($Authors as $n => $val)
        {          $item = array();
          $item['title'] = $val['title'];
          $item['link'] = $BaseURL.$arr['code'].'/'.$val['linkname'].$CONFIG['webPageFileType'];
          $item['code'] = $val['linkname'];
          $item['items'] = array();
          $SiteMap[$k]['items'][] = $item;
        }
      }
      elseif($arr['code'] == 'regions')
      {
        $Items = getTableAsArray($_SQL_TABLE['region'], 'title'.__FLANG, array("active = 'y'"), '', 'id,linkname,title'.__FLANG.' AS title');
        foreach($Items as $n => $val)
        {
          $item = array();
          $item['title'] = $val['title'];
          $item['link'] = $BaseURL.$arr['code'].'/'.$val['linkname'].$CONFIG['webPageFileType'];
          $item['code'] = $val['linkname'];
          $item['items'] = array();
          $SiteMap[$k]['items'][] = $item;
        }
      }
      elseif($arr['code'] == 'rubrics')
      {
        $Items = getTableAsArray($_SQL_TABLE['rubric'], 'title'.__FLANG, array("active = 'y'"), '', 'id,linkname,title'.__FLANG.' AS title');
        foreach($Items as $n => $val)
        {
          $item = array();
          $item['title'] = $val['title'];
          $item['link'] = $BaseURL.$arr['code'].'/'.$val['linkname'].$CONFIG['webPageFileType'];
          $item['code'] = $val['linkname'];
          $item['items'] = array();
          $SiteMap[$k]['items'][] = $item;
        }
      }
      elseif($arr['code'] == 'tags')
      {
        $Items = getTableAsArray($_SQL_TABLE['keyword'], 'title'.__FLANG, array("active = 'y'"), '', 'id,linkname,title'.__FLANG.' AS title');
        foreach($Items as $n => $val)
        {
          $item = array();
          $item['title'] = $val['title'];
          $item['link'] = $BaseURL.$arr['code'].'/'.$val['linkname'].$CONFIG['webPageFileType'];
          $item['code'] = $val['linkname'];
          $item['items'] = array();
          $SiteMap[$k]['items'][] = $item;
        }
      }
    }


    /*
    $Articles = getTableAsArray($_SQL_TABLE['article'], 'title'.__FLANG, array("active = 'y'", "uismp = 'y'"), '', 'id,linkname,title'.__FLANG.' AS title');
    foreach($Articles as $n => $val)
    {
      $item = array();
      $item['title'] = $val['title'];
      $item['link'] = $BaseURL.'articles/'.$val['linkname'].$CONFIG['webPageFileType'];
      $item['code'] = $val['linkname'];
      $item['items'] = array();
      $SiteMap[] = $item;
    }*/


  $tpl->assign("SiteMap", $SiteMap);
  $tpl->assign("PageTitle", $language['front']['siteMap']);
  $_BODY_SMARTY_TEMPLATE = $CONFIG['ModulesFolder']."module.sitemap.tpl";

?>